# Copyright (C) 2006-2008 by Sergio Pistone
# sergio_pistone@yahoo.com.ar
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

require File.expand_path( File.dirname( __FILE__ ) + "/../utils/strings" )
require File.expand_path( File.dirname( __FILE__ ) + "/../i18n/i18n" )
require File.expand_path( File.dirname( __FILE__ ) + "/../gui/gui" )
require File.expand_path( File.dirname( __FILE__ ) + "/pluginadapter" )
require File.expand_path( File.dirname( __FILE__ ) + "/plugins" )

require "md5"
require "uri"

module WikiPluginAdapter

	# Hack to make module methods become class methods when the module gets included
	def WikiPluginAdapter.included( including )
		if including.is_a?( Class )
			including.extend( ClassMethods ) # adds class methods
		else # including.is_a?( Module )
			including::ClassMethods.append_class_methods( self )
		end
	end

	# Methods under this module will became class methods when the module gets included
	# Note: don't use def self.<method name> but just <method name>
	module ClassMethods
		def ClassMethods.append_class_methods( mod )
			include mod::ClassMethods
		end
	end

	include PluginAdapter

	def submit?()
		return @wpa_submit
	end

	def check_submit_conditions()
 		if @wpa_submit
			login( @wpa_username, @wpa_password, false )
		else
			logout()
		end

		if ! logged_in?
			@wpa_submit = false
		end

		if ! @wpa_submit || ! @wpa_review
			@wpa_prompt_no_lyrics = false
			@wpa_prompt_autogen = false
		end
	end

	def set_submit_settings( username, password, review, prompt_autogen, prompt_no_lyrics )
		@wpa_submit = true
		@wpa_review = review
		@wpa_prompt_autogen = prompt_autogen
		@wpa_prompt_no_lyrics = prompt_no_lyrics
		@wpa_username = username
		@wpa_password = password
		if ! @wpa_submit || ! @wpa_review
			@wpa_prompt_no_lyrics = false
			@wpa_prompt_autogen = false
		end
	end

	# returns true if the review contents page is shown to the user (which can only happen when there was something to submit)
	def wiki_process_response( response, response_plugin, wiki_searched )

		return false if ! submit?() || ! authorized?

		lyrics_found_on_site = response.lyrics && response_plugin == self
		lyrics_autogenerated = lyrics_found_on_site && ! response.custom_data["reviewed"]

		return false if lyrics_found_on_site && ! lyrics_autogenerated # we have nothing to do
		return false if (! @wpa_prompt_autogen && lyrics_autogenerated) || (! @wpa_prompt_no_lyrics && ! response.lyrics)

		begin

			check_submit_conditions()

			song_data = MediaWikiLyrics::SongData.new(
				response.artist ? response.artist : response.request.artist,
				response.title ? response.title : response.request.title,
				response.lyrics,
				response.album ? response.album : response.request.album,
				response.year ? response.year : response.request.year,
				response.custom_data["credits"],
				response.custom_data["lyricist"]
			)

			submitted_url, submitted_data = submit_song_page(
				song_data,
				lyrics_autogenerated ? response.url : nil, # are we editing an existing page or creating a new one?
				song_data.artist == response.request.artist && song_data.title == response.request.title &&
				wiki_searched && ! lyrics_found_on_site, # do we KNOW FOR SURE the page doesn't exists?
				@wpa_review || lyrics_autogenerated, # should we show the review contents dialog?
				lyrics_autogenerated # should user be forced to review contents? (true causes the operation to abort otherwise)
			)

			# transfer summited data to response
			if submitted_data
				response.artist = submitted_data["artist"] if ! Strings.empty?( submitted_data["artist"] )
				response.title = submitted_data["title"] if ! Strings.empty?( submitted_data["title"] )
				response.album = submitted_data["album"] if ! Strings.empty?( submitted_data["album"] )
				response.year = submitted_data["year"] if ! Strings.empty?( submitted_data["year"] )
				response.lyrics = submitted_data["lyrics"] if ! Strings.empty?( submitted_data["lyrics"] )
				custom_data = {}
				custom_data.merge( response.custom_data )
				custom_data["credits"] = submitted_data["credits"] if ! Strings.empty?( submitted_data["credits"] )
				custom_data["lyricst"] = submitted_data["lyricst"] if ! Strings.empty?( submitted_data["lyricst"] )
				response.custom_data = custom_data
			end

			return (@wpa_review || lyrics_autogenerated) && submitted_data

		rescue TimeoutError
			notify( I18n.get( "cli.application.plugintimeout", plugin_name(), site_host() ) )
			return false
		end

	end

end
